// 通用工具函数
import { createHash } from "node:crypto";

const UNUSABLE_PASSWORD = "!";
/**
 * getHexDigest 加密字符串
 *
 * @param {string} algorithm 加密算法
 * @param {string} salt 随机字符串
 * @param {string} raw 明文
 *
 * @returns {string} 密文
 */
export function getHexDigest(algorithm, salt, raw) {
  let hash = createHash(algorithm);
  hash.update(salt + raw);
  return hash.digest("hex");
}

/**
 * makePassword 密码加密函数
 *
 * @param {string} rawPassword 明文密码
 * @returns 加密后的密文
 */
export function makePassword(rawPassword) {
  if (rawPassword == undefined) return UNUSABLE_PASSWORD;
  let algo = "sha1",
    rand = () => Math.random().toString(16).slice(-8),
    salt = getHexDigest(algo, rand(), rand()).slice(-8),
    hsh = getHexDigest(algo, salt, rawPassword);
  return `${algo}$${salt}$${hsh}`;
}

/**
 * checkPassword 验证密码
 *
 * @param {string} rawPassword 明文密码
 * @param {string} encPassword 密文
 * @returns {boolean}
 */
export function checkPassword(rawPassword, encPassword) {
  if (encPassword == UNUSABLE_PASSWORD) return false;
  let [algo, salt, hsh] = encPassword.split("$");
  if (!algo || !salt || !hsh) return false;
  return hsh == getHexDigest(algo, salt, rawPassword);
}
